Using the RDC with Visual C++

There are different ways to access the Report Designer Component (RDC) and any other COM Automation server through Visual C++. This section describes how to use the #import method.

Manipulating the RDC in Microsoft Visual C++ involves three steps:

The following section leads you through the details in each of these steps.

Printing a Report through Visual C++

To print a report through Visual C++
  1. Open Visual C++ 6.0 if it isn't already running.
  2. From the File menu, select New. In the New dialog box select the MFC AppWizard (exe) from the Projects tab. Type in MyRDC for the Project Name and click OK.
  3. Click the Finish button to accept the defaults for the MFC AppWizard and then click OK in the New Project Information dialog box.
  4. Add a reference to the RDC runtime object Model. From the File|Open menu select the StdAfx.h file that was generated by the MFC App wizard and Click Open. Add the following line after the #include directives:
#import "C:\Program Files\Seagate Software\Crystal Reports\Developer 
Files\include\craxdrt.tlb" 

Note:    This is the default location of the RDC runtime object model (craxdrt.dll).

  1. Before you can invoke an RDC object, you must initialize OLE. From the File|Open menu, open the MyRDC.CPP file and add the following code:
struct InitOle {
InitOle()  { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize();   }
} _init_InitOle_;
  1. For optional Variant parameters declare a dummy variable in the MyRDC2.CPP class.
Variant dummy;
  1. Add the following constants in the declaration's of the MyRDC2.CPP class:
// The constants needed to create the Application and Report Objects COM 
objects
const CLSID CLSID_Application = 
{0xb4741fd0,0x45a6,0x11d1,{0xab,0xec,0x00,0xa0,0xc9,0x27,0x4b,0x91}};
const IID IID_IApplication = 
{0x0bac5cf2,0x44c9,0x11d1,{0xab,0xec,0x00,0xa0,0xc9,0x27,0x4b,0x91}};
const CLSID CLSID_ReportObjects = 
{0xb4741e60,0x45a6,0x11d1,{0xab,0xec,0x00,0xa0,0xc9,0x27,0x4b,0x91}};
const IID IID_IReportObjects = 
{0x0bac59b2,0x44c9,0x11d1,{0xab,0xec,0x00,0xa0,0xc9,0x27,0x4b,0x91}};

Opening and Printing a Crystal Report through the RDC

To open and print a Crystal Report through the RDC
  1. From the File|Open menu open the MyRDC.CPP file. Add the following code to the MyRDC.InitInstance() method just before the return statement:
// A dummy variant
VariantInit (&dummy);
dummy.vt = VT_EMPTY;
HRESULT                hr = S_OK;
            IApplicationPtr m_Application = NULL;
            IReportPtr m_Report = NULL;
            
            // Specify the path to the report you want to print                                    
_bstr_t ReportPath("c:\\Program Files\\Seagate Software\\Crystal 
Reports\\Samples\\En\\Reports\\General Business\\Inventory.rpt");
_variant_t vtEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);
// Instantiate the IApplication object
hr = CoCreateInstance(CLSID_Application, NULL, CLSCTX_INPROC_SERVER , 
IID_IApplication, (void **) & CRAXDRT::IApplication);
//Open the Report using the OpenReport method
m_Report = m_Application->OpenReport(ReportPath, dummy)
//Print the Report to printer
m_Report->PrintOut(dummy, dummy, dummy, dummy
);
  1. Finally, from the Build menu, select Rebuild All.

There are hundreds of properties, methods, and events in the Report Designer Component object model that you can manipulate through your code to meet the demands of virtually any reporting requirement.



Seagate Software IMG Holdings, Inc.
http://www.seagatesoftware.com
Support services:
http://support.seagatesoftware.com